home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
8206
/
8206.xpi
/
content
/
rss
/
request.js
< prev
next >
Wrap
Text File
|
2010-02-02
|
4KB
|
186 lines
function WiseStampCreateXMLHttpRequest()
{
var xmlhttp = new XMLHttpRequest();
return {
xmlhttp: xmlhttp,
readyState: 0,
responseXML: null,
responseText: "",
onsuccess: null,
onfailure: null,
url: "",
sent: "",
async: 1,
onsuccesscalled: false,
makeOnReadyStateChangeHandler: function ()
{
var obj = this;
return function ()
{
obj.readyState = obj.xmlhttp.readyState;
if (obj.xmlhttp.readyState == 4)
{
var status = '';
try
{
status = obj.xmlhttp.status;
} catch(e)
{
obj.onerror(e);
return;
};
if (status == 200)
{
//WiseStampUtils.log("request.js :: makeOnReadyStateChangeHandler :: status = 200, obj.xmlhttp.responseText = " + obj.xmlhttp.responseText);
obj.responseXML = obj.xmlhttp.responseXML;
obj.responseText = obj.xmlhttp.responseText;
if (obj.onsuccess)
{
obj.onsuccesscalled = true;
obj.onsuccess();
};
};
};
obj.onreadystatechange();
};
},
makeOnErrorHandler: function ()
{
var obj = this;
return function (e)
{
obj.xmlhttp = new XMLHttpRequest();
obj.xmlhttp.onerror = function ()
{
obj.onfailure();
};
obj.xmlhttp.onreadystatechange = obj.makeOnReadyStateChangeHandler();
obj.xmlhttp.open(obj._open.method, obj._open.url, obj._open.mode);
obj.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
obj.xmlhttp.send(obj.sent);
};
},
onreadystatechange: function ()
{},
open: function (method, url, mode)
{
this._open =
{
method: method,
url: url,
mode: mode
};
var obj = this;
this.onerror = this.makeOnErrorHandler();
this.xmlhttp.onerror = this.makeOnErrorHandler();
this.xmlhttp.onreadystatechange = this.makeOnReadyStateChangeHandler();
var ts = new Date;
if (url.indexOf('?') == -1)
{
url = url + "?_ts=" + ts.getTime();
} else
{
url = url + "&_ts=" + ts.getTime();
};
this.url = url;
this.async = mode;
this.xmlhttp.open(method, url, mode);
this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
},
send: function (data)
{
this.sent = data;
this.xmlhttp.overrideMimeType("text/xml");
this.xmlhttp.send(data);
// There's a difference in FF and IE behavior:
// IE calls onreadystatechange even foe non-async requests,
// while FF doesn't; thus, the following condition a
// a workaround for FF behavior
if (!this.async && !this.onsuccesscalled && this.xmlhttp.readyState == 4)
{
this.responseXML = this.xmlhttp.responseXML;
this.responseText = this.xmlhttp.responseText;
this.onsuccesscalled = true;
if (this.onsuccess)
{
this.onsuccess();
};
};
},
handleError: function (callbacks)
{
// FireFox bug workaround;
// sometimes FF (up to 1.5.0.1) XMLHTTPRequest object
// returns empty response body whatever the actual response was
// (when calling from the FCKEditor event handler).
if (this.xmlhttp.responseText == "" && this.xmlhttp.responseXML == null)
{
return false;
};
if (!this.xmlhttp.responseXML)
{
alert("Error; server responded with '" + this.xmlhttp.responseText + "'");
return true;
};
if (!this.xmlhttp.responseXML.documentElement)
{
alert("Error; server responded with '" + this.xmlhttp.responseText + "'");
return true;
};
if (this.xmlhttp.responseXML.documentElement.tagName == "parsererror")
{
alert("Error; server responded with '" + this.xmlhttp.responseText + "'");
return true;
};
var root = this.xmlhttp.responseXML.documentElement;
if (root.tagName == 'error')
{
var handler = null;
var code = root.getAttribute('code');
var text = root.text ? root.text : root.textContent;
if (callbacks != null)
{
handler = callbacks[code];
};
if (handler)
{
return handler(code, text);
} else
{
var query_text = this.sent.replace(/&/g, "\n");
alert("ERROR: " + code + " " + text + "\n" + query_text);
return true;
};
};
return false;
}
}
};